package dome2;

import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    public Queue<Integer> queue1;
    public Queue<Integer> queue2;
    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if(empty()) {
            queue1.offer(x);
        }else if(!queue1.isEmpty()) {
            queue1.offer(x);
        }else {
            queue2.offer(x);
        }
    }

    public int pop() {
        if(empty()) {
            return -1;
        }
        if(!queue1.isEmpty()) {
            while(queue1.size()!=1) {
                int tmp = queue1.poll();
                queue2.offer(tmp);
            }
            return queue1.poll();
        }else {
            while(queue2.size()!=1) {
                int tmp = queue2.poll();
                queue1.offer(tmp);
            }
            return queue2.poll();
        }
    }

    public int top() {
        if(empty()) {
            return -1;
        }
        int tmp = -1;
        if(!queue1.isEmpty()) {
            while(queue1.size()!=0) {
                tmp = queue1.poll();
                queue2.offer(tmp);
            }
        }else {
            while(queue2.size()!=1) {
                tmp = queue2.poll();
                queue1.offer(tmp);
            }
            return queue2.poll();
        }
        return tmp;
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}